package gogohome.ourhome;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.zip.CRC32;
import java.util.zip.Checksum;

import android.widget.Toast;

public class makepacketdata {
		byte[] method1(byte protocol_id,String format,byte[] ... args) {
			byte[] result = new byte[50];
	    	int length = 0;
	    	int i = 0;
	    	int l = 0;
	    	//byte[] cbyte = new byte[1];
	    	//byte[] hbyte = new byte[2];
	    	byte[] sbyte = new byte[30];
	    	
	    	//String fieldTypes = args[0].toString();
	    	String fieldTypes = format;
	    	length = args.length;
	    	for(i=0;i < length;i++){
	    		//if(i!= 0)
	    			switch(fieldTypes.charAt(i)){
	    			case 'c':
	    				sbyte = args[i];
	    				result[l] = sbyte[0];
	    				l += 1;
	    				//result = result.acbyte.toString();
	    				//list.toArray();
	    				//result.add(t[0]);
	    				//result.add(args[0].toString().);    				
	    				break;
	    			case 'h':
	    				sbyte = args[i];
	    				result[l] = sbyte[0];
	    				result[l+1] = sbyte[1];
	    				l += 2;
	    				//result.add(sbyte[0]);
	    				//result.add(sbyte[1]);
	    				//t = args[2].getBytes();
	    				//result.add(args[2].getBytes());
	    				break;  
	    			case 'd':
	    				sbyte = args[i];
	    				//sbyte = args[i].getBytes();
	    				result[l] = sbyte[0];
	    				result[l+1] = sbyte[1];
	    				result[l+2] = sbyte[2];
	    				result[l+3] = sbyte[3];
	    				l += 4;
	    				//result.add(sbyte[0]);
	    				//result.add(sbyte[1]);
	    				//result.add(sbyte[2]);
	    				//result.add(sbyte[3]);
	    				break;
	    			case 's':
	    				
	    				int Stringlength = args[i].length;
	    				sbyte = args[i];
	    				int j=0;
	    				for(j=0; j< Stringlength;j++)
	    				{
	    					//result = sbyte[j]
	    					  result[l] = sbyte[j];
	    					  l += 1;
	    					//result.add(sbyte[j]);
	    				}
	    				//result.add(null);
	    				result[l] = 0;
	    				break;
	    				
	    			}
	    		    	    		    	
	    		    	
	    		    	
	    		    	
	    				/*
	    		    	return new byte[]{
	    					(byte)((crc_data>>24)&0xff),
	    					(byte)((crc_data>>16)&0xff),
	    					(byte)((crc_data>>8)&0xff),
	    					(byte)((crc_data>>0)&0xff),    						
	    				};
	    				*/
	    				//return 
//	    			}
//	    		    result[l] = 
	    			
	    			
	    		//else
	    		//{
	    			//fieldTypes = args[0].toString();
	    		//}
	    		
	    		
	    	}
	    	
	    	
	    	Checksum crc = new CRC32();
		      ////////////////////////////////////////////////////////////////////////////////
//		      BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename));
		      //byte buffer[] = new byte[32768];
		      long crc_data;
		      int length2 = 0;
		      length2 = result.length;
		        crc.update(result, 0, l);
		        
//		      in.close();
		      ////////////////////////////////////////////////////////////////////////////////
		        crc_data = crc.getValue();
//  		    public static byte[] toByte(long data){
		    	result[l] = (byte)((crc_data>>24)&0xff);
		    	result[l+1] = (byte)((crc_data>>16)&0xff);
		    	result[l+2] = (byte)((crc_data>>8)&0xff);
		    	result[l+3] = (byte)((crc_data>>0)&0xff);
		    	l+=4;

	    	byte[] fbyte = new byte[l+5];
	    	System.arraycopy(result,0,fbyte,5,l);
	    	fbyte[4]=protocol_id;
	    	System.arraycopy(intToByteArray(l-4),0,fbyte,0,4);
			return fbyte; 
		}
		
		byte[] crc32_Enc(byte[] data){
			Checksum crc32 = new CRC32();
			long crc_data;
		    int length2 = 0;
		    length2 = data.length;
		    crc32.update(data,0 , 1);
	        crc_data = crc32.getValue();
	        byte[] crcbyte = new byte[4];
//		    public static byte[] toByte(long data){
	        crcbyte[0] = (byte)((crc_data>>24)&0xff);
	        crcbyte[1] = (byte)((crc_data>>16)&0xff);
	        crcbyte[2] = (byte)((crc_data>>8)&0xff);
	        crcbyte[3] = (byte)((crc_data>>0)&0xff);		    		    
			return crcbyte;
		}
		
		 public static long crc32_Enc(InputStream in) throws IOException {
			    Checksum cs = new CRC32();
			    // more efficient to read chunks of data at a time
			    for (int b = in.read(); b != -1; b = in.read()) {
			      cs.update(b);
			    }
			    return cs.getValue();
		 }
/*		
		int crc32_chk(byte[] in_data){
			
			
			return 1;			
		}
*/		
		/*
	    List<Byte> method1(String... args) {
	 
	    	List<Byte> result = new ArrayList<Byte>();
	    	//ArrayList<Byte> list = new ArrayList<Byte>();
	    	int length = 0;
	    	int i = 0;
	    	//byte[] cbyte = new byte[1];
	    	//byte[] hbyte = new byte[2];
	    	byte[] sbyte = new byte[50];
	    	
	    	String fieldTypes = args[0].toString();
	    	for(i=0;i < args.length;i++){
	    		if(i!= 0)
	    			switch(fieldTypes.charAt(i-1)){
	    			case 'c':
	    				sbyte = args[i].getBytes();
	    				result.add(sbyte[0]);
	    				//result = result.acbyte.toString();
	    				//list.toArray();
	    				//result.add(t[0]);
	    				//result.add(args[0].toString().);    				
	    				break;
	    			case 'h':
	    				sbyte = args[i].getBytes();
	    				result.add(sbyte[0]);
	    				result.add(sbyte[1]);
	    				//t = args[2].getBytes();
	    				//result.add(args[2].getBytes());
	    				break;
	    			case 'd':
	    				sbyte = args[i].getBytes();
	    				result.add(sbyte[0]);
	    				result.add(sbyte[1]);
	    				result.add(sbyte[2]);
	    				result.add(sbyte[3]);
	    				break;
	    			case 's':
	    				
	    				int Stringlength = args[i].toString().length();
	    				sbyte = args[i].getBytes();
	    				int j=0;
	    				for(j=0; j< Stringlength;j++)
	    				{
	    					result.add(sbyte[j]);
	    				}
	    				result.add(null);
	    				break;
	    				
	    			}
	    		else
	    		{
	    			fieldTypes = args[0].toString();
	    		}    			
	    		
	    	}
	    	
	    	
	      System.out.println(Arrays.asList(args) +
	        " // " + args.length);
		return result;
	    }
	    */
	    private static void method2(int arg, Object... args) {
	      System.out.println(Arrays.asList(args) + " / " + arg);
	    }
	    private static byte[] intToByteArray(final int integer) {    
	    	ByteBuffer buff = ByteBuffer.allocate(Integer.SIZE / 8);    
	    	buff.putInt(integer);    
	    	buff.order(ByteOrder.BIG_ENDIAN);    
	    	return buff.array();}	
		public static byte[] toByte(long data){
			return new byte[]{		
				(byte)((data>>24)&0xff),
				(byte)((data>>16)&0xff),
				(byte)((data>>8)&0xff),
				(byte)((data>>0)&0xff),
					
			};
			//return 
		}	    							
	}

